package serverwork;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author sw
 * @project minicat
 * @description
 * @date 2020/5/5 23:23
 */
public class SocketWork extends Thread {
    private ServerSocket serverSocket;
    private ThreadPoolExecutor threadPoolExecutor;
    private ApplicationInfo applicationInfo;

    public SocketWork(ServerSocket serverSocket,ThreadPoolExecutor threadPoolExecutor,ApplicationInfo applicationInfo){
        this.serverSocket=serverSocket;
        this.threadPoolExecutor=threadPoolExecutor;
        this.applicationInfo=applicationInfo;
    }

    @Override
    public void run() {
        while (true) {
            System.out.println("minicat start port " + applicationInfo.getPort());
            Socket socket = null;
            try {
                socket = serverSocket.accept();
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
            Runnable runnable = new RequestWorkProcess(socket, applicationInfo);
            threadPoolExecutor.execute(runnable);
        }
    }
}
